<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/common/hospital_token.jsp"%>
<%
    // 薪资查询
    Integer userId = input.getInt("user_id", token.getUid());
    String date = input.getDesc("date", DateUtil.formatDateByFormat(new Date(), "yyyyMM"));
    HospitalUser hospitalUser = UserUtil.getUser(hospital, userId);
    if (hospitalUser == null) {
        ErrcodeUtil.setErrcode(msg, ErrcodeUtil.NO_USER);
        Tools.encryptPrint(out, msg);
        return;
    }
    String phone = Tools.getDesc(hospitalUser.getPhone(), "");
    if (phone.isEmpty()) {
        ErrcodeUtil.setErrcode(msg, ErrcodeUtil.NO_RESULT);
        Tools.encryptPrint(out, msg);
        return;
    }
    List<String> jsonStringList = QBuilder.db("basedb").select("sm.json as json").from("salary_module sm").where(new Where("sm.hospital=", hospital).and("sm.date=", date).and("sm.phone=", phone)).page(1, 1).query().getResultObj();
    if (jsonStringList == null || jsonStringList.isEmpty()) {
        ErrcodeUtil.setErrcode(msg, ErrcodeUtil.NO_RESULT);
        Tools.encryptPrint(out, msg);
        return;
    }
    String jsonString = jsonStringList.get(0);
    Map jsonMap = JSONUtil.fromJson(jsonString, new TypeReference<Map>(){});
    List<Map> dataList = (List<Map>)jsonMap.get("data");
    if (dataList == null || dataList.isEmpty()) {
        ErrcodeUtil.setErrcode(msg, ErrcodeUtil.NO_RESULT);
        Tools.encryptPrint(out, msg);
        return;
    }
    Map inFactPayroll = null; // 实发工资
    Map shouldPayroll = null; // 应发合计
    Map chargePayroll = null; // 扣款合计
    List<Map> levelTopList = new ArrayList<Map>();
    Iterator<Map> iterator = dataList.iterator();
    while(iterator.hasNext()) {
        Map dataMap = iterator.next();
        String level = Tools.getDesc(dataMap.get("level"), "0");
        if ("1".equals(level)) {
            iterator.remove();
            String nameInDataMap = Tools.getDesc(dataMap.get("name"), "");
            if (nameInDataMap.contains("实发工资")) {
                inFactPayroll = dataMap;
                continue;
            } else if (nameInDataMap.contains("应发合计")) {
                shouldPayroll = dataMap;
                continue;
            } else if (nameInDataMap.contains("扣款合计")) {
                chargePayroll = dataMap;
                continue;
            }
            levelTopList.add(dataMap);
        }
    }
    List<Map> newLevelTopList = new ArrayList<Map>();
    if (inFactPayroll != null) {
        newLevelTopList.add(inFactPayroll);
    }
    if (shouldPayroll != null) {
        newLevelTopList.add(shouldPayroll);
    }
    if (chargePayroll != null) {
        newLevelTopList.add(chargePayroll);
    }
    newLevelTopList.addAll(levelTopList);
    Map data = new HashMap();
    data.put("top", newLevelTopList);
    data.put("down", dataList);
    msg.put("data", data);
    Tools.encryptPrint(out, msg);
%>
